﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common;
using Common.Views;
using iTextSharp.text;
using System.IO;
using iTextSharp.text.pdf;

namespace ElectrosLtdApplication.Controllers
{
    public class OrdersController : MenusController
    {
        //
        // GET: /Orders/

        public ActionResult Index()
        {
            List<OrdersView> products = new OrdersServ.OrdersServiceClient().GetProductsBoughtByUser(HttpContext.User.Identity.Name).ToList();
            return View("Index", products);
        }

        [Authorize]
        public void AddComment(int pId, string comment)
        {
            new ProductsServ.ProductsServiceClient().AddComment(HttpContext.User.Identity.Name, pId, comment);
        }

        [Authorize]
        public string AddRating(int pId, int rating)
        {
            string value = new ProductsServ.ProductsServiceClient().AddRatingForProduct(HttpContext.User.Identity.Name, pId, rating);
            return value;
        }

        [Authorize]
        public PartialViewResult GetProductDetails(int pId, Guid oId)
        {
            OrdersView details = new ProductsServ.ProductsServiceClient().GetProductDetails(pId, oId);
                details.FaultStatus = new FaultsServ.FaultsServiceClient().GetFaultsForOrder(oId, pId);
            return PartialView("_details", details);
        }

        [Authorize]
        private Document CreatePDF(List<OrdersView> products, MemoryStream ms)
        {
            Document doc = new Document();
            PdfWriter writer = PdfWriter.GetInstance(doc, ms);
            doc.Open();
            PdfPTable table = new PdfPTable(6); //New table with 6 columns

            //Adding a title
            PdfPCell header = new PdfPCell(new Phrase("Order Details"));
            header.Colspan = 6; //Merge the cell
            header.HorizontalAlignment = 1; //Center align
            header.Border = 0; //Remove border
            table.AddCell(header);

            //Adding the headers
            table.AddCell("Product ID");
            table.AddCell("Product Name");
            table.AddCell("Quantity");
            table.AddCell("Price per product");
            table.AddCell("Date Purchased");
            table.AddCell("Warranty Expiry Date");

            //Adding details for each product
            foreach (OrdersView p in products)
            {
                table.AddCell(p.ProductID.ToString());
                table.AddCell(p.ProductName);
                table.AddCell(p.Quantity.ToString());
                table.AddCell("€" + p.Price.ToString());
                table.AddCell(p.OrderDate.ToShortDateString());
                table.AddCell(p.WarrantyExpiryDate.ToShortDateString());
            }

            //Adding the table to the PDF document
            doc.Add(table);
            writer.CloseStream = false;
            doc.Close();
            ms.Flush();
            ms.Position = 0;

            return doc;
        }

        [Authorize]
        public FileStreamResult PrintDetails()
        {
            string username = HttpContext.User.Identity.Name;
            MemoryStream ms = new MemoryStream();
            List<OrdersView> products = new OrdersServ.OrdersServiceClient().GetProductsBoughtByUser(username).ToList();
            Document doc = CreatePDF(products, ms);
            Guid fileName = Guid.NewGuid();
            Response.Clear();
            //To display pdf in browser
            Response.AppendHeader("content-disposition", "inline; filename="+fileName+".pdf");
            return new FileStreamResult(ms, "application/pdf");
        }

        [Authorize]
        public void SaveDetails()
        {
            string username = HttpContext.User.Identity.Name;
            new OrdersServ.OrdersServiceClient().SaveDetails(username);
        }
    }
}
